package cn.heygod.nacostool.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.dialect.PropsUtil;
import com.alibaba.nacos.api.PropertyKeyConst;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @Author Simon
 */
@Configuration
@ConfigurationProperties(prefix = "spring.cloud.nacos.discovery")
@Data
public class NacosConfig {

    public static final String DEFAULT_CLUSTER_NAME = "DEFAULT";
    public static final String DEFAULT_NAMESPACE = "public";


    private String namespace;

    private String serverAddr;

    private String clusterName;

    private String group;

    private String username;

    private String password;

    private String accessKey;
    private String secretKey;

    public Properties createProps(){
        Properties properties = new Properties();

        //环境信息
        put(properties, PropertyKeyConst.NAMESPACE,namespace , DEFAULT_NAMESPACE);
        put(properties, PropertyKeyConst.CLUSTER_NAME,clusterName,DEFAULT_CLUSTER_NAME);
        put(properties, PropertyKeyConst.SERVER_ADDR,serverAddr);
        //账号信息
        put(properties, PropertyKeyConst.USERNAME,username);
        put(properties, PropertyKeyConst.PASSWORD,password);
        //Key信息--有用户就不需要此
        put(properties, PropertyKeyConst.ACCESS_KEY,accessKey);
        put(properties, PropertyKeyConst.SECRET_KEY,secretKey);

        return properties;
    }

    private void put(Properties properties,String key,String val){
       put(properties,key,val,null);
    }

    private void put(Properties properties,String key,String val,String defaultVal){
        if(StrUtil.isNotBlank(val)){
            properties.put(key,val);
        }else if(defaultVal !=null ){
            //如果有传默认值，则采用默认值
            properties.put(key,defaultVal);
        }
    }



}
